package com.shisan.tingshu.order.receiver;

import com.alibaba.fastjson.JSONObject;
import com.shisan.tingshu.common.execption.ShisanException;
import com.shisan.tingshu.common.rabbit.constant.MqConst;
import com.shisan.tingshu.common.util.MD5;
import com.shisan.tingshu.order.service.MqOpsService;
import com.rabbitmq.client.Channel;
import com.shisan.tingshu.order.service.OrderInfoService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;


@Component
@Slf4j
public class OrderInfoReceiver {

    @Autowired
    private MqOpsService mqOpsService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private OrderInfoService orderInfoService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_LOCAL_MSG, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_LOCAL_MSG, durable = "true"),
            key = MqConst.ROUTING_LOCAL_MSG))
    @SneakyThrows
    public void listenUpdateLocalMsgStatus(String content, Message message, Channel channel) {

        // 1.判断消息是否存在
        if (StringUtils.isEmpty(content)) {
            return;  // 不用消费
        }
        // 2.消费消息
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        mqOpsService.updateLocalMsgStatus(content);
        // 4.手动应答消息（将消息从队列中删除掉）
        channel.basicAck(deliveryTag, false);
    }

    @RabbitListener(queues = {MqConst.QUEUE_CANCEL_ORDER})
    @SneakyThrows
    public void listenCancelOrder(String orderNo, Message message, Channel channel) {

        // 1.判断消息是否存在
        if (StringUtils.isEmpty(orderNo)) {
            return;  // 不用消费
        }
        // 2.消费消息
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        mqOpsService.cancelOrder(orderNo); // 关单
        // 4.手动应答消息（将消息从队列中删除掉）
        channel.basicAck(deliveryTag, false);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_ORDER_PAY_SUCCESS, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_ORDER, durable = "true"),
            key = MqConst.ROUTING_ORDER_PAY_SUCCESS))
    @SneakyThrows
    public void listenWxPaySuccess(String content, Message message, Channel channel) {

        // 1.判断消息是否存在
        if (StringUtils.isEmpty(content)) {
            return;  // 不用消费
        }
        // 2.处理消息
        Map<String, String> map = JSONObject.parseObject(content, Map.class);
        String orderNo = map.get("orderNo");
        String userId = map.get("userId");
        String msgMd5 = MD5.encrypt(orderNo);
        // 3.消费消息
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            orderInfoService.PaySuccess(Long.parseLong(userId), orderNo);  // 直接调用之前零钱支付成功的后续操作
            // 4.手动应答消息（将消息从队列中删除掉）
            channel.basicAck(deliveryTag, false);
        } catch (ShisanException e) {
            String msgRetryKey = "msg:retry:" + msgMd5;
            Long count = redisTemplate.opsForValue().increment(msgRetryKey);
            // 三次重试
            if (count >= 3) {
                log.error("消息已经到达了重试{}次，请人工排查错误原因：{}", count, e.getMessage());
                // 不能重试
                channel.basicNack(deliveryTag, false, false);
                redisTemplate.delete(msgRetryKey);
            } else {
                log.info("消息重试{}次", count);
                channel.basicNack(deliveryTag, false, true);
            }
        } catch (Exception e) {
            log.error("签收消息时网络出现了故障，异常原因：{}", e.getMessage());
            channel.basicNack(deliveryTag, false, false);
        }

    }
}
